<?php

namespace backend\modules\home\models;

use Yii;

/**
 * This is the model class for table "mh_advertlist".
 *
 * @property int $id
 * @property string $title
 * @property string $desc
 * @property string $url
 * @property string $img_path
 * @property string $start
 * @property string $finish
 * @property int $click_num
 * @property int $status
 * @property int $create_at
 */
class Advertlist extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mh_advertlist';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'start', 'finish'], 'required'],
            [['click_num', 'create_at', 'status'], 'integer'],
            [['title', 'desc', 'start', 'finish'], 'string', 'max' => 20],
            [['url', 'img_path'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '标题',
            'desc' => '描述',
            'url' => '跳转链接',
            'img_path' => '图片',
            'start' => '开始时间',
            'finish' => '结束时间',
            'click_num' => '点击量',
            'status' => '状态',
            'create_at' => '创建时间',
        ];
    }

    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            if($this->isNewRecord){
                $this->create_at = time();
                $this->click_num = 0;
            }
            return true;
        }
        return false;
    }
}
